package com.ebraga.trackerclub.android.view.fragment;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.ebraga.trackerclub.android.R;
import com.ebraga.trackerclub.android.handler.service.RankingServiceHandler;
import com.ebraga.trackerclub.android.util.DLog;
import com.ebraga.trackerclub.android.util.ISmartServiceConstants;
import com.ebraga.trackerclub.android.util.SmartServiceJavascriptInterface;
import com.ebraga.trackerclub.android.util.api.AndroidContext;
import com.ebraga.trackerclub.android.util.core.AndroidContextImpl;
import com.ebraga.trackerclub.android.view.MainActivity;
import com.ebraga.trackerclub.android.view.MyWebViewClient;
import com.ebraga.trackerclub.android.vo.RankingVO;

/**
 * Fragment that appears in the "content_frame"
 */
@SuppressLint("SetJavaScriptEnabled")
public class RankingFragment extends Fragment {
	WebView webView;
	
    public RankingFragment() {
        // Empty constructor required for fragment subclasses
    	
    }


	private RankingServiceHandler rankingServiceHandler() {
		
		AndroidContext context = new AndroidContextImpl();
		   
		context.setContext(getActivity().getApplicationContext());				

		RankingServiceHandler rankingServiceHandler = new RankingServiceHandler(context);
		
		return rankingServiceHandler;
		
	}        
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	
    	((MainActivity)getActivity()).setCurrentScreen(ISmartServiceConstants.SCREEN_TAG.RANKING);
    	
    	View mainView = (View) inflater.inflate(R.layout.fragment_webview, container, false);
        webView = (WebView) mainView.findViewById(R.id.webview);
        webView.setWebViewClient(new MyWebViewClient());
        webView.getSettings().setBuiltInZoomControls(false);
        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new JavascriptInterface(inflater.getContext()), "AndroidNativeCode");
       	webView.loadUrl("file:///android_asset/ranking.html");
        
        return mainView;
    }
    
    
	public class JavascriptInterface extends SmartServiceJavascriptInterface {
		 
		public JavascriptInterface(Context c) {
			super(c);
		}
		
		public void getRanking() {

			
			try {

				RankingServiceHandler rankingServiceHandler = rankingServiceHandler();		
				List<RankingVO> rankingVOs = rankingServiceHandler.retrieveRanking();
				
				
				JSONArray listJson = new JSONArray();
				JSONObject jObject;
				int position = 1;
				for (RankingVO rankingVO : rankingVOs) {
					
					jObject = new JSONObject();
					jObject.put("Position", position);
					jObject.put("Name", rankingVO.getName());
					jObject.put("Speed", rankingVO.getSpeed());
					jObject.put("Model", rankingVO.getModel());
					
					listJson.put(jObject);
					position++;
					
				}
				

				webView.loadUrl("javascript:setHistoryCheckInOut(" + listJson + ")");
				
			} catch (Exception e) {
				DLog.e(e);
			}			
			

		}		
		
	}

}